from flask import Flask, request, jsonify
from flask_cors import CORS
from util import generate_word_cloud
from predict import predict
app = Flask(__name__)
CORS(app)


@app.route('/api/process', methods=['POST'])
def process():
    if request.is_json:
        data = request.get_json()
        comment = data.get('comment')
        print(comment)
        keyword, predicted_labels, view,clusters = predict(comment)
        # 输入comment 输出count keyword tendency
        count = len(keyword)-1  # todo
        predicted_labels = list(map(int, predicted_labels))
        print(predicted_labels)
        text_tendency = predicted_labels[0]# todo
        print(view)
        print(clusters)
        # keyword = ['你好', '你不好', '好好好']  # todo
        tendency = predicted_labels[1:]  # todo
        print(len(clusters), len(clusters),len(tendency))
        result = {'count': count, 'text_tendency': text_tendency, 'keyword': keyword, 'tendency': tendency}
        generate_word_cloud(view, [1]*len(clusters),clusters)
        return jsonify(result), 200
    else:
        result = {'error': 'request is not json'}
        return jsonify(result), 400


if __name__ == '__main__':
    app.run(port=8000)
